﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;
using System.Web;

namespace YouHP.Pages.Member.Group
{
    public class Member : GroupBasePage
    {
        protected List<GroupUserInfo> AdminList { get; set; }
        protected List<GroupUserInfo> MemberList { get; set; }
        protected List<GroupUserInfo> ApplyList { get; set; }
        protected int PageCount = 1;
        protected int PageIndex = 1;
        protected int PageSize = 15;
        protected int MemberCount = 0;
        protected string PageNumberString = "";

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.AddLocation("用户管理中心", "/User/Index.aspx");
            this.AddLocation("圈子成员管理", "/User/Group/Memeber.aspx");
        }

        protected new void HandlePost()
        {
            int type = RequestTools.GetFormInt("formtype", 0);
            if (type == 1) HandleApplyForm();
            else if (type == 2) HandleAdminForm();
            else if (type == 3) HandleMemberForm();
        }

        protected void HandleApplyForm()
        {
            int applytype = RequestTools.GetFormInt("applytype", 0);
            string idstr = RequestTools.GetFormString("applyid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (applytype == 1)  // 通过审批
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleAppling);
            }
            else if (applytype == 2)　// 忽略
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleIngore);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                this.AddMsgLine(idstr);
                this.AddMsgLine(applytype.ToString());
                this.SetUrl(RequestTools.GetUrlReferrer());
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected void HandleAdminForm()
        {
            int admintype = RequestTools.GetFormInt("admintype", 0);
            string idstr = RequestTools.GetFormString("adminid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (admintype == 1)  // 将为普通成员
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleMember);
            }
            else if (admintype == 2)　// 删除
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleDeleted);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                this.AddMsgLine(idstr);
                this.AddMsgLine(admintype.ToString());
                this.SetUrl(RequestTools.GetUrlReferrer());
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected void HandleMemberForm()
        {
            int membertype = RequestTools.GetFormInt("membertype", 0);
            string idstr = RequestTools.GetFormString("memberid");
            int[] idArray = Utils.StrToIntArray(idstr, ",");
            bool succ = false;
            if (membertype == 1)  // 提升为管理员
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleAdmin);
            }
            else if (membertype == 2)　// 删除
            {
                succ = GroupUserBLL.Instance.UpdateGroupUserRole(GroupData.Id, idArray, Constants.RoleDeleted);
            }
            if (succ)
            {
                this.AddMsgLine("操作成功");
                this.AddMsgLine(idstr);
                this.AddMsgLine(membertype.ToString());
                this.SetUrl(RequestTools.GetUrlReferrer());
            }
            else
            {
                this.AddErrLine("操作失败");
            }
        }

        protected new void HandleGet()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);
            AdminList = GroupUserBLL.Instance.GetGroupUserListAdmin(GroupData.Id);
            MemberList = GroupUserBLL.Instance.GetGroupUserListMember(GroupData.Id, PageSize, PageIndex, out MemberCount);
            ApplyList = GroupUserBLL.Instance.GetGroupUserListAppling(GroupData.Id);
            PageCount = Utils.GetPageCount(MemberCount,PageSize);
            string url = string.Format("Member.aspx?gid={0}",GroupData.Id);
            PageNumberString = Utils.GetPageNumbers(PageIndex, PageCount, url, 8, "page", null);
        }
    }
}